package com.eclipse.org.snms.summary;

import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Text;


public class ViewStickyNoteForm extends ApplicationWindow {

	public ViewStickyNoteForm() {
		super(null);
	}
	protected Control createContents(final Composite parent) {
		
		parent.getShell().setBounds(100, 100, 350, 500);
		parent.setLayout(new GridLayout(1,false));

		/////////////////////////////////////////////
		// Adds fields for sticky note info
		/////////////////////////////////////////////
		addStickyNoteFields(parent);
		
		/////////////////////////////////////////////
		// Adds components for step task info
		/////////////////////////////////////////////
		addStepsComponents(parent);
		
		/////////////////////////////////////////////
		// Adds two main buttons (Save and Cancel)
		////////////////////////////////////////////
		addButtons(parent);
		
	    return parent;
	}
	
	/////////////////////////////////////
	// Fields for sticky notes
	/////////////////////////////////////
	public void addStickyNoteFields(Composite parent){
		
		Group bGroup = new Group(parent,SWT.NONE);
		bGroup.setLayout(new GridLayout(2,false));
		bGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		bGroup.setText("Sticky Note Info");
		
		final String[] LABEL_STRINGS = {"Title:","Priority:","Deadline:", "Description:"};
		Label[] Labels = new Label[4];
		Text[] tFields = new Text[4];
		
		for (int i=0; i< Labels.length - 1; i++){
			Labels[i] = new Label(bGroup, 0);
			Labels[i].setText(LABEL_STRINGS[i]);
			tFields[i] = new Text(bGroup,SWT.BORDER);
			tFields[i].setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
			tFields[i].setSize(300, 25);
		}
		Labels[3]= new Label(bGroup,0);
		Labels[3].setText(LABEL_STRINGS[3]);
		tFields[3] = new Text(bGroup,SWT.MULTI | SWT.WRAP | SWT.V_SCROLL | SWT.BORDER);
		tFields[3].setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		tFields[3].setSize(300, 50);
	}
	
	/////////////////////////////////////
	//Component to add the task steps
	/////////////////////////////////////
	public void addStepsComponents(final Composite parent){
		addStepsEntryComponents(parent);
		addStepsDisplayComponents(parent);
	}
	
	/////////////////////////////////////
	//Component to enter task steps
	/////////////////////////////////////
	public void addStepsEntryComponents(final Composite parent){
		
		Group bGroup = new Group(parent,SWT.NONE);
		bGroup.setLayout(new GridLayout(2,false));
		bGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		bGroup.setText("Add Steps");
		
		/////////////////
		// Check Button
		/////////////////
		Button addTaskCheck= new Button(bGroup, SWT.CHECK);
		addTaskCheck.setText("Add task");
		
		final Text stepText = new Text(bGroup, SWT.BORDER);
		stepText.setSize(200,20);
		stepText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		stepText.setEnabled(false);
		
		addTaskCheck.addSelectionListener(new SelectionAdapter() {
		      public void widgetSelected(SelectionEvent event) {
		          boolean preserveCase = ((Button) event.widget).getSelection();
		          stepText.setEnabled(preserveCase);
		        }});
	
		Button addButton = new Button(bGroup,0);
		addButton.setText("Add step");
		
		addButton.addListener(SWT.Selection, new Listener(){
			public void handleEvent(Event event) {
				final MessageBox mb = new MessageBox(parent.getShell(), SWT.ICON_INFORMATION | SWT.OK);
				mb.setText("Save");
				mb.setMessage("Task step added successfully!");
				mb.open();
			}});
	}
	
	/////////////////////////////////////
	//Component to display the task steps
	/////////////////////////////////////
	public void addStepsDisplayComponents(final Composite parent){
		Group bGroup = new Group(parent,SWT.NONE);
		bGroup.setLayout(new GridLayout(2,false));
		bGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		bGroup.setText("Steps");
		
		Label[] Labels = new Label[5];
		Text[] tFields = new Text[5];
		
		for (int i=0; i< Labels.length; i++){
			Labels[i] = new Label(bGroup, 0);
			Labels[i].setText(" "+(i+1)+" ");
			tFields[i] = new Text(bGroup,SWT.BORDER);
			tFields[i].setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
			tFields[i].setSize(200, 20);
		}
	}
	public void addButtons(final Composite parent){
		Group bGroup = new Group(parent,SWT.NONE);
		bGroup.setLayout(new GridLayout(3,false));
		bGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		final MessageBox mb = new MessageBox(parent.getShell(), SWT.ICON_INFORMATION | SWT.OK);
		mb.setText("Save");
		mb.setMessage("Sticky note saved successfully!");
		
		Button saveButton = new Button(bGroup, SWT.PUSH | SWT.CENTER);
		GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
		gridData.widthHint = 60;
		saveButton.setLayoutData(gridData);
		saveButton.setText("Save");
		saveButton.addListener(SWT.Selection, new Listener(){
			public void handleEvent(Event event) {
				mb.open();
			}});
		
		// Hidden label as a separator between buttons.
		Label hLabel = new Label(bGroup,0);
		hLabel.setBounds(0, 60, 80, 20);
		hLabel.setText("\t\t\t");
		hLabel.setVisible(false);
		
		Button cancelButton = new Button(bGroup, SWT.PUSH | SWT.CENTER);
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_END);
		gridData.widthHint = 60; 
		cancelButton.setLayoutData(gridData);
		cancelButton .setText("Cancel");
		cancelButton .addListener(SWT.Selection, new Listener(){
			public void handleEvent(Event event) {
				parent.getShell().close();
			}});
	}
}
